package dtec.project.whereareu;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Register extends Activity {
	String us,pa;
	boolean logchk=false;
	Button login,regis;
	EditText username,password;
	String DURL=MainActivity.URL+"/mapproject/newuser.php";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register);
		login=(Button)findViewById(R.id.button1);
		username=(EditText)findViewById(R.id.editText1);
		password=(EditText)findViewById(R.id.editText2);
		login.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				goLogin();
			}
		});

	}
	public void goLogin(){
		us=username.getText().toString();
		pa=password.getText().toString();
	//	Toast.makeText(Login.this,input[0].toString(), Toast.LENGTH_LONG).show();
		new letLogin().execute(us,pa);
	}

	
	//network class
	private class letLogin extends AsyncTask<String ,String ,String > {


		@Override
		protected String doInBackground(String... params) {
			String urlparam = null;
			// TODO Auto-generated method stub
			try {
				urlparam=URLEncoder.encode("uname","UTF-8")+"="+URLEncoder.encode(params[0].toString(),"UTF-8");
				urlparam=urlparam+"&"+URLEncoder.encode("upass","UTF-8")+"="+URLEncoder.encode(params[1].toString(),"UTF-8");
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			String s=sendHttpPost(urlparam,DURL);
			return s;
		}
	    protected void onPostExecute(String result) {
	    	//Login.flag=result;
	    	Toast.makeText(Register.this,result.trim().substring(0), Toast.LENGTH_LONG).show();
	    	if(result.trim().substring(0).equals("y")){
	    		Toast.makeText(Register.this,"Register successful", Toast.LENGTH_LONG).show();
	    		logchk=true;
	    		if(logchk==true){
	    		    Intent intent = new Intent(Register.this, Login.class);
	    		    finish();
	    		    startActivity(intent);
	    		}
	    	}
	    	else if(result.trim().substring(0).equals("n")){
	    		Toast.makeText(Register.this,"Same username", Toast.LENGTH_LONG).show();
	    	}
	    	else{
	    		Toast.makeText(Register.this,"error", Toast.LENGTH_LONG).show();
	    	}
	    }
	    public String sendHttpPost(String data,String u){
	    	String result=null;
	    	try {
				URL url=new URL(u);
				try {
					
					///set up
					HttpURLConnection cont=(HttpURLConnection)url.openConnection();
					cont.setRequestMethod("POST");
					cont.setConnectTimeout(6*1000);
					cont.setDoInput(true);
					cont.setDoOutput(true);
					cont.setUseCaches(false);
					
					///send
					DataOutputStream out=new DataOutputStream(cont.getOutputStream());
					out.writeBytes(data);
					out.flush();
					out.close();
					
					///get result
					InputStream in=cont.getInputStream();
					BufferedReader readin=new BufferedReader(new InputStreamReader(in,"UTF-8"));
					String line;
					StringBuffer respon=new StringBuffer();
					while((line=readin.readLine())!=null){
						respon.append(line);
						respon.append("\n");
					}
					readin.close();
					result=respon.toString();
					
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	    	return result;
	    }
		
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_register, menu);
		return true;
	}

}
